package com.fau.ss.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.ss.action.PublicAction;
import com.fau.ss.entity.MedicalAdmin;
import com.fau.ss.entity.MedicalSsPurchase;
import com.fau.ss.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 消费习惯统计Action
 * @author liy
 * @datetime 2014年11月3日,下午2:54:07
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class BuyFrequencyStaAction extends PublicAction implements ModelDriven<MedicalSsPurchase> {

	private static final long serialVersionUID = -4743165361566354326L;

	private MedicalSsPurchase purchase = new MedicalSsPurchase();
	
	/**
	 * @desc 进入消费习惯统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年10月30日,下午9:54:13
	 */
	@Action(value = "buy_frequency_init", results = {@Result(name = SUCCESS, location = "buy_frequency_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入基本统计页面
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月10日,上午9:56:07
	 */
	@Action(value = "buy_frequency_basic", results = {@Result(name = SUCCESS, location = "buy_frequency_basic.jsp")})
	public String basic() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询购基本统计数据
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月11日,上午11:09:47
	 */
	@Action(value = "buy_frequency_basic_view")
	public String basicView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getBuyFrequencyStaService().findHabitsByPage(map, purchase, admin);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 进入药品类别统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午1:43:56
	 */
	@Action(value = "buy_frequency_medical", results = {@Result(name = SUCCESS, location = "buy_frequency_medical.jsp")})
	public String medical(){
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询药品类别统计数据
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月13日,下午2:44:17
	 */
	@Action(value = "buy_frequency_medical_view")
	public String medicalView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getBuyFrequencyStaService().findHabitsMedicineByPage(map, purchase, admin);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 进入年龄段统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,上午11:37:17
	 */
	@Action(value = "buy_frequency_age", results = {@Result(name = SUCCESS, location = "buy_frequency_age.jsp")})
	public String age(){
		return SUCCESS;
	}
	
	/**
	 * @desc 年龄段统计报表(柱状图)
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月11日,下午1:59:56
	 */
	@Action(value = "buy_frequency_age_view")
	public String ageView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getBuyFrequencyStaService().countByAge(purchase, admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 进入年度统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,上午11:44:11
	 */
	@Action(value = "buy_frequency_year", results = {@Result(name = SUCCESS, location = "buy_frequency_year.jsp")})
	public String year(){
		return SUCCESS;
	}
	
	/**
	 * @desc 年度统计报表(柱状图)
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月11日,下午5:03:53
	 */
	@Action(value = "buy_frequency_year_view")
	public String yearView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getBuyFrequencyStaService().countByYear(purchase, admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 进入月份统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午1:36:25
	 */
	@Action(value = "buy_frequency_month", results = {@Result(name = SUCCESS, location = "buy_frequency_month.jsp")})
	public String month(){
		return SUCCESS;
	}
	
	/**
	 * @desc 月份统计报表(柱状图)
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月12日,下午12:06:01
	 */
	@Action(value = "buy_frequency_month_view")
	public String monthView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getBuyFrequencyStaService().countByMonth(purchase, admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 进入会员等级统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午2:10:20
	 */
	@Action(value = "buy_frequency_level", results = {@Result(name = SUCCESS, location = "buy_frequency_level.jsp")})
	public String level(){
		return SUCCESS;
	}
	
	/**
	 * @desc 会员等级统计报表(柱状图)
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月12日,下午12:06:01
	 */
	@Action(value = "buy_frequency_level_view")
	public String levelView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = this.baseAllService.getBuyFrequencyStaService().countByLevel(purchase, admin);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 进入宣传区域统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午2:32:06
	 */
	@Action(value = "buy_frequency_area", results = {@Result(name = SUCCESS, location = "buy_frequency_area.jsp")})
	public String area(){
		return SUCCESS;
	}

	/**
	 * @desc 宣传区域统计报表(柱状图)
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月12日,下午12:06:01
	 */
	@Action(value = "buy_frequency_area_view")
	public String areaView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = this.baseAllService.getBuyFrequencyStaService().countByArea(purchase, admin);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 消费习惯基本统计导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月30日,下午11:01:11
	 */
	@Action(value = "buy_frequency_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"消费习惯基本统计导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getPurchasingPowerStaService().buyFrequencyExportBasicAll(this.purchase);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 消费习惯药品类别统计导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月30日,下午11:30:02
	 */
	@Action(value = "buy_frequency_ms_export_all")
	public String medicineSortAllExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"消费习惯药品类别统计导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getPurchasingPowerStaService().buyFrequencyMsExportBasicAll(this.purchase);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalSsPurchase getModel() {
		return purchase;
	}
	
}
